//
//  UserIMNet.m
//  SportSocial
//
//  Created by yull on 14/12/23.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "UserIMNet.h"
#import "IM_Service+Write.h"
#import "AppDelegate.h"

@implementation UserIMNet

+ (instancetype)shareInstance
{
    static UserIMNet *imNet = nil;
    static dispatch_once_t predicte;
    dispatch_once(&predicte, ^{
        imNet = [[UserIMNet alloc] init];
    });
    return imNet;
}

- (instancetype)init
{
    if (self = [super init]) {
        NSArray *pidArray = @[[NSNumber numberWithInteger:USER_Login_Receive_PID],
                          [NSNumber numberWithInteger:USER_KICK_PID]];
        [self.arrayPid addObjectsFromArray:pidArray];
    }
    return self;
}

#pragma mark - 发消息
- (void)login
{
    IM_Service *service =[IM_Service instance];
    
    if (service.canConnect && service.isConnected) {
        NSString *token = [CurrentUser shareInstance].token;
        UInt64 userId = [CurrentUser shareInstance].userId;
        NSString *friendMd5 = [CurrentUser shareInstance].friendMd5;
        
        LoginCGMessage_Builder *login_builder = [LoginCGMessage builder];
        login_builder = [login_builder setToken:token];
        login_builder = [login_builder setUserId:userId];
        login_builder = [login_builder setFriendMd5:friendMd5];
        
        for (GroupInfo *groupInfo in [CurrentUser shareInstance].groupList) {
            
            GroupMd5_Builder *groupMd5_builder = [GroupMd5 builder];
            groupMd5_builder = [groupMd5_builder setGroupId:groupInfo.groupId];
            groupMd5_builder = [groupMd5_builder setGroupMd5:groupInfo.md5];
            
            login_builder = [login_builder addGroupMd5:[groupMd5_builder build]];
        }
        
        LoginCGMessage *loginMessage = [login_builder  build];
        
        NSData *data = [loginMessage data];
        
        [service sendData:data protocolId:USER_Login_Send_PID];
    }
}

#pragma mark - 收消息
- (void)handlePacketWithPid:(NSInteger)pid recvData:(NSData *)recvData
{
    switch (pid) {
        case USER_Login_Receive_PID:
            [self handleLoginWithRecvData:recvData];
            break;
        case USER_KICK_PID:
            [self handleKick];
            break;
        default:
            [super handlePacketWithPid:pid recvData:recvData];
            break;
    }
}

//登陆返回
- (void)handleLoginWithRecvData:(NSData *)recvData
{
    LoginGCMessage *loginMessage = [LoginGCMessage parseFromData:recvData];
    
    if (loginMessage.loginGcmessageResult == 1) {
        NSLog(@"登陆成功!");
    }else if (loginMessage.loginGcmessageResult == 2) {
        NSLog(@"token不对重新登陆!");
        [self handleKick];
    }else {
        NSLog(@"重新登陆!");
    }
}

//被踢出
- (void)handleKick
{
    [CurrentUser shareInstance].isLogin = NO;
    
    [[DBManager shareInstance] closeDataBase];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UserLogOut" object:nil];
        
        [CurrentUser shareInstance].isLogin = NO;
        
        [[IM_Service instance] disconnectService];
        
        [(AppDelegate *)[UIApplication sharedApplication].delegate logOut];
    });
}
@end
